using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using FreeLancer.HRB.Core.Services;

namespace FreeLancer.HRB.Core.Web.HttpHandlers
{
  /// <summary>
  /// Language Handler
  /// </summary>
  public class Language : IHttpHandler, IRequiresSessionState
  {
    #region IHttpHandler Members

    /// <summary>
    /// Is Reusable
    /// </summary>
    public bool IsReusable
    {
      get
      {
        return true;
      }
    }

    /// <summary>
    /// Process request.
    /// </summary>
    /// <param name="context">Context</param>
    public void ProcessRequest(HttpContext context)
    {
      HttpRequest request = context.Request;
      HttpResponse responce = context.Response;

      string culture = string.Empty;
      culture = request.QueryString[QueryCultureParam];
      if (string.IsNullOrEmpty(culture))
        culture = WebAppSettings.Instance.Culture;

      Language.SetCulture(context, culture);

      // Redirect to the original web page.
      responce.Redirect(request.UrlReferrer.AbsolutePath); 
    }

    #endregion

    #region Gets/Sets Culture

    /// <summary>
    /// Sets the current thread culture.
    /// </summary>
    /// <param name="cultureName">Name of the culture as it is described in the documentation of CultureInfo.</param>
    protected static void SetCulture(string cultureName)
    {
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
      Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    }

    /// <summary>
    /// Gets the current application culture.
    /// </summary>
    /// <param name="context">Context</param>
    /// <returns></returns>
    public static string GetCulture(HttpContext context)
    {
      string culture = WebAppSettings.Instance.Culture;
      // Get the current application language.
      if (context.Session != null)
      {
        if (context.Session[CultureParam] != null)
        {
          return (string)context.Session[CultureParam];
        }
      }

      return culture;
    }

    /// <summary>
    /// Sets the current application culture.
    /// </summary>
    /// <param name="context">Context</param>
    /// <param name="culture">Culture</param>
    public static void SetCulture(HttpContext context, string culture)
    {
      string currentCulture = culture;
      try
      {
        Language.SetCulture(culture);
      }
      catch (Exception ex)
      {
        currentCulture = WebAppSettings.Instance.Culture;
        Language.SetCulture(currentCulture);
        
        LoggingService.LogError("Invalid language", ex,
          LoggingService.DefaultCategory, null);
      }

      // Set the current application language.
      context.Session[CultureParam] = currentCulture;
    }

    #endregion

    #region Constants

    /// <summary>
    /// The session variable name.
    /// </summary>
    public const string CultureParam = "Culture";

    /// <summary>
    /// The GET request parameter name.
    /// </summary>
    public const string QueryCultureParam = "CultureId";

    #endregion
  }
}
